import Vue from 'vue';

Vue.directive('allow', {
  inserted(el, binding) {
    const currentUser = JSON.parse(window.sessionStorage.getItem('currentUserAdmin'));
    let currentPrivileges = currentUser.privileges;
    if (currentPrivileges === 'all') return;
    currentPrivileges = currentPrivileges.split(',').map(x => Number(x));
    let requiredPrivileges = binding.value;
    if (_.isNumber(requiredPrivileges)) requiredPrivileges = [requiredPrivileges];
    const hasPrivilege = _.intersection(currentPrivileges, requiredPrivileges).length > 0;
    if (!hasPrivilege) {
      el.parentNode.removeChild(el);
    }
  }
});
